package spring;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

import trip.*;
import dao.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
public class WelcomeController {
	
	private Person_tripDAO ptD = Factory.getInstance().getPerson_tripDAO();
	private CompanyDAO cD = Factory.getInstance().getCompanyDAO();
	private PersonDAO pD = Factory.getInstance().getPersonDAO();
	private RouteDAO rD = Factory.getInstance().getRouteDAO();
	private Route_stopsDAO rsD = Factory.getInstance().getRoute_stopsDAO();
	private TripDAO tD = Factory.getInstance().getTripDAO();
	private StopDAO sD = Factory.getInstance().getStopDAO();
	private Time_tableDAO ttD = Factory.getInstance().getTime_tableDAO();
	
	
	@RequestMapping(value = "/error", method = RequestMethod.GET)
	public String error(Model model) {
		return "error";
	}
	
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public String main(Model model) {
		try {
			List<Trip> trips = tD.getActive();
			trips = trips.subList(0, trips.size() < 3 ? trips.size() : 3);
			List<Stop> stops = new ArrayList<Stop>();
			List<String> strings = new ArrayList<String>();
			List<Time_table> tts = new ArrayList<Time_table>();
			List<Integer> seats = new ArrayList<Integer>();
			SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm");
			for (Trip t : trips) {
				stops = sD.getByRoute(t.getRoute());
				tts = ttD.getByTrip(t);
				strings.add(stops.get(0).getName());
				strings.add(stops.get(stops.size() - 1).getName());
				strings.add(sdf.format(tts.get(0).getDeparture()));
				strings.add(sdf.format(tts.get(tts.size()-1).getArrival()));
				seats.add(ttD.getVacantSeats(stops.get(0), stops.get(stops.size() - 1), t));
			}
			model.addAttribute("trips", trips);
			model.addAttribute("strings", strings);
			model.addAttribute("seats", seats);
			return "main";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/order", method = RequestMethod.GET)
	public String doOrder(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			Trip trip = tD.getById(id);
			List<Stop> stops = sD.getByRoute(trip.getRoute());
			model.addAttribute("trip", trip);
			model.addAttribute("stops", stops);
			List<Integer> seats = new ArrayList<Integer>();
			List<Integer> price = new ArrayList<Integer>();
			List<Time_table> tts = ttD.getByTrip(trip);
			for (Time_table tt : tts) {
				seats.add(tt.getVacantSeats());
				price.add(tt.getPrice());
			}
			model.addAttribute("seats", seats);
			model.addAttribute("price", price);
			model.addAttribute("persons", pD.getAll());
			model.addAttribute("orders", pD.getByTrip(trip));
			return "order";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/order", method = RequestMethod.POST)
	public String doOrderPost(@RequestParam(value="id", required=true) Integer id,
			 			  @ModelAttribute("email") String email,
					      @ModelAttribute("st1") Integer st1,
						  @ModelAttribute("st2") Integer st2, Model model) {
		try {
			Trip trip = tD.getById(id);
			List<Stop> stops = sD.getByRoute(trip.getRoute());
			Person person = pD.getByEmail(email);
			ptD.create(new Person_trip(1, person , trip, stops.get(st1 - 1), stops.get(st2 - 1)));
			return "redirect:person_order?id=" + String.valueOf(person.getId());
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trips", method = RequestMethod.GET)
	public String getTrips(Model model) {
		try {
			List<Trip> trips = tD.getAll();
			List<Stop> stops = new ArrayList<Stop>();
			List<String> strings = new ArrayList<String>();
			List<Time_table> tts = new ArrayList<Time_table>();
			List<Integer> seats = new ArrayList<Integer>();
			SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm");
			for (Trip t : trips) {
				stops = sD.getByRoute(t.getRoute());
				tts = ttD.getByTrip(t);
				strings.add(stops.get(0).getName());
				strings.add(stops.get(stops.size() - 1).getName());
				strings.add(sdf.format(tts.get(0).getDeparture()));
				strings.add(sdf.format(tts.get(tts.size()-1).getArrival()));
				Date date = tts.get(0).getDeparture();
				date.setTime(date.getTime() + 1);
				if (date.after(new Date()) && t.getRoute().getActive())
					strings.add(new String("Да"));
				else
					strings.add(new String("Нет"));
				seats.add(ttD.getVacantSeats(stops.get(0), stops.get(stops.size() - 1), t));
			}
			model.addAttribute("trips", trips);
			model.addAttribute("strings", strings);
			model.addAttribute("seats", seats);
			return "trips";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip", method = RequestMethod.GET)
	public String getTrip(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			Trip trip = tD.getById(id);
			List<Stop> route = sD.getByRoute(trip.getRoute());
			model.addAttribute("trip", trip);
			model.addAttribute("route", route);
			List<String> strings = new ArrayList<String>();
			List<Time_table> tts = ttD.getByTrip(trip);
			SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm");
			for (Time_table tt : tts) {
				strings.add(sdf.format(tt.getDeparture()));
				strings.add(sdf.format(tt.getArrival()));
				strings.add(tt.getPrice().toString());
			}
			Date date = tts.get(0).getDeparture();
			date.setTime(date.getTime() + 1);
			model.addAttribute("strings", strings);
			if (date.after(new Date()) && trip.getRoute().getActive())
				model.addAttribute("order", 1);
			else
				model.addAttribute("order", 0);
			return "trip";
		} catch (HibernateException ex) {
			return "error";
		}
	}

	@RequestMapping(value = "/trip_add", method = RequestMethod.GET)
	public String addTrip(Model model) {
		try {
			model.addAttribute("comps", cD.getAll());
			List<Route> active = rD.getActive();
			model.addAttribute("routes", active);
			List<List<String>> stops = new ArrayList<List<String>>(); 
			for (Route rt : active) {
				List<Stop> tmp = sD.getByRoute(rt);
				List<String> tmps = new ArrayList<String>();
				for (Stop st : tmp)
					tmps.add(st.getName());
				stops.add(tmps);
			}
			model.addAttribute("stops", stops);
			return "trip_add";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip_add", method = RequestMethod.POST)
	public String addTripPost(@ModelAttribute("trip") Trip trip, 
							  @ModelAttribute("rnumber") Integer rnumber,
							  @ModelAttribute("cid") Integer cid, 
							  @ModelAttribute("info") ListWrapper<String, String> info, Model model) {
		try {
			List<Route> active = rD.getActive();
			trip.setRoute(active.get(rnumber)).setCompany(cD.getById(cid));
			tD.create(trip);
			List<String> price = info.getList();
			List<String> times = info.getSec();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
			for (int i = 0; i <= price.size(); i++) {
				Time_table tt = new Time_table();
				if (i == 0)
					tt.setPrice(0).setDeparture(sdf.parse(times.get(0))).setArrival(sdf.parse(times.get(0)));
				if (i > 0 && i < price.size())
					tt.setPrice(Integer.parseInt(price.get(i - 1))).
					setDeparture(sdf.parse(times.get(i * 2))).setArrival(sdf.parse(times.get(i * 2 - 1)));
				if (i == price.size())
					tt.setPrice(Integer.parseInt(price.get(i - 1))).
					setDeparture(sdf.parse(times.get(i * 2 - 1))).setArrival(sdf.parse(times.get(i * 2 - 1)));
				tt.setStop_number(i + 1);
				tt.setTrip(trip);
				ttD.create(tt);
			}
			return "redirect:trips";
		} catch (HibernateException ex) {
			return "error";
		} catch (ParseException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip_edit", method = RequestMethod.GET)
	public String editTrip(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			Trip trip = tD.getById(id);
			model.addAttribute("trip", trip);
			model.addAttribute("comps", cD.getAll());
			List<Route> active = rD.getActive();
			model.addAttribute("routes", active);
			List<List<String>> stops = new ArrayList<List<String>>(); 
			for (Route rt : active) {
				List<Stop> tmp = sD.getByRoute(rt);
				List<String> tmps = new ArrayList<String>();
				for (Stop st : tmp)
					tmps.add(st.getName());
				stops.add(tmps);
			}
			model.addAttribute("stops", stops);
			List<String> info = new ArrayList<String>();
			List<Time_table> tts = ttD.getByTrip(trip);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
			for (Time_table tt : tts) {
				info.add(sdf.format(tt.getDeparture()));
				info.add(sdf.format(tt.getArrival()));
				info.add(tt.getPrice().toString());
			}
			model.addAttribute("info", info);
			return "trip_edit";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip_edit", method = RequestMethod.POST)
	public String editTripPost(@RequestParam(value="id", required=true) Integer id,
							   @ModelAttribute("trip") Trip trip, 
							   @ModelAttribute("rnumber") Integer rnumber,
							   @ModelAttribute("cid") Integer cid, 
							   @ModelAttribute("info") ListWrapper<String, String> info, Model model) {
		try {
			Trip old = tD.getById(id);
			List<Route> active = rD.getActive();
			List<String> price = info.getList();
			List<String> times = info.getSec();
			List<Time_table> oldtts = ttD.getByTrip(old);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
			trip.setCompany(cD.getById(cid)).setSeats(old.getSeats());
			trip.setRoute(rnumber == - 1 ? old.getRoute() : active.get(rnumber));
			if (rnumber != -1 && active.get(rnumber).getId() != old.getRoute().getId()) {
				tD.create(trip);
				if (rnumber != -1) {
					for (int i = 0; i <= price.size(); i++) {
						Time_table tt = new Time_table();
						if (i == 0)
							tt.setPrice(0).setDeparture(sdf.parse(times.get(0))).setArrival(sdf.parse(times.get(0)));
						if (i > 0 && i < price.size())
							tt.setPrice(Integer.parseInt(price.get(i - 1))).
							setDeparture(sdf.parse(times.get(i * 2))).setArrival(sdf.parse(times.get(i * 2 - 1)));
						if (i == price.size())
							tt.setPrice(Integer.parseInt(price.get(i - 1))).
							setDeparture(sdf.parse(times.get(i * 2 - 1))).setArrival(sdf.parse(times.get(i * 2 - 1)));
						tt.setStop_number(i + 1);
						tt.setTrip(trip);
						ttD.create(tt);
					}
				} 
				else {
					for(Time_table tt : oldtts) {
						tt.setTrip(trip);
						ttD.create(tt);
					}
				}
			}
			else {
				tD.update(trip);
				if (rnumber != -1) {
					for (int i = 0; i < oldtts.size(); i++) {
						Time_table tt = oldtts.get(i);
						if (i == 0)
							tt.setPrice(0).setDeparture(sdf.parse(times.get(0))).setArrival(sdf.parse(times.get(0)));
						if (i > 0 && i < price.size())
							tt.setPrice(Integer.parseInt(price.get(i - 1))).
							setDeparture(sdf.parse(times.get(i * 2))).setArrival(sdf.parse(times.get(i * 2 - 1)));
						if (i == price.size())
							tt.setPrice(Integer.parseInt(price.get(i - 1))).
							setDeparture(sdf.parse(times.get(i * 2 - 1))).setArrival(sdf.parse(times.get(i * 2 - 1)));
						ttD.update(tt);
					}
				}
			}
			return "redirect:trips";
		} catch (HibernateException ex) {
			return "error";
		} catch (ParseException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip_search", method = RequestMethod.GET)
	public String searchTrip(Model model) {
		try {
			model.addAttribute("comps", cD.getAll());
			model.addAttribute("stops", sD.getAll());
			List<Trip> trips = tD.getAll();
			List<Stop> stops = new ArrayList<Stop>();
			List<String> strings = new ArrayList<String>();
			List<Time_table> tts = new ArrayList<Time_table>();
			List<Integer> seats = new ArrayList<Integer>();
			SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm");
			for (Trip t : trips) {
				stops = sD.getByRoute(t.getRoute());
				tts = ttD.getByTrip(t);
				strings.add(stops.get(0).getName());
				strings.add(stops.get(stops.size() - 1).getName());
				strings.add(sdf.format(tts.get(0).getDeparture()));
				strings.add(sdf.format(tts.get(tts.size()-1).getArrival()));
				Date date = tts.get(0).getDeparture();
				date.setTime(date.getTime() + 1);
				if (date.after(new Date()) && t.getRoute().getActive())
					strings.add(new String("Да"));
				else
					strings.add(new String("Нет"));
				seats.add(ttD.getVacantSeats(stops.get(0), stops.get(stops.size() - 1), t));
			}
			model.addAttribute("trips", trips);
			model.addAttribute("strings", strings);
			model.addAttribute("seats", seats);
			return "trip_search"; 
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip_search", method = RequestMethod.POST)
	public String searchTripPost(@ModelAttribute("strings") ListWrapper<String, String> info, Model model) {
		try {
			model.addAttribute("comps", cD.getAll());
			model.addAttribute("stops", sD.getAll());
			List<String> strings = new ArrayList<String>();
			List<Stop> stops = new ArrayList<Stop>();
			List<Time_table> tts = new ArrayList<Time_table>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
			List<String> search = info.getList();
			List<String> comps = info.getSec();
			List<Integer> seats = new ArrayList<Integer>();
			Date in =  search.get(0) == "" ? null : sdf.parse(search.get(0));
			Date out = search.get(1) == "" ? null : sdf.parse(search.get(1));
			Stop dep = search.get(2) == "" ? null : sD.getById(Integer.parseInt(search.get(2)));
			Stop arr = search.get(3) == "" ? null : sD.getById(Integer.parseInt(search.get(3)));
			Integer minp = search.get(4) == "" ? null : Integer.parseInt(search.get(4));
			Integer maxp = search.get(5) == "" ? null : Integer.parseInt(search.get(5));
			Integer mins = search.get(6) == "" ? null : Integer.parseInt(search.get(6));
			Integer maxs = search.get(7) == "" ? null : Integer.parseInt(search.get(7));
			Integer rnum = search.get(8) == "" ? null : Integer.parseInt(search.get(8));
			Integer seatnum = search.get(9) == "" ? null : Integer.parseInt(search.get(9));
			List<Company> compl = new ArrayList<Company>();
			for (String str : comps) {
				if (str != null) 
					compl.add(cD.getById(Integer.parseInt(str)));
			}
			List<Trip> trips = tD.getByFilters(in, out, dep, arr, seatnum, minp, maxp, mins, maxs, rnum, compl);
			for (Trip t : trips) {
				stops = sD.getByRoute(t.getRoute());
				tts = ttD.getByTrip(t);
				strings.add(stops.get(0).getName());
				strings.add(stops.get(stops.size() - 1).getName());
				strings.add(sdf.format(tts.get(0).getDeparture()));
				strings.add(sdf.format(tts.get(tts.size()-1).getArrival()));
				Date date = tts.get(0).getDeparture();
				date.setTime(date.getTime() + 1);
				if (date.after(new Date()) && t.getRoute().getActive())
					strings.add(new String("Да"));
				else
					strings.add(new String("Нет"));
				seats.add(ttD.getVacantSeats(stops.get(0), stops.get(stops.size() - 1), t));
			}
			model.addAttribute("trips", trips);
			model.addAttribute("strings", strings);
			model.addAttribute("seats", seats);
			return "trip_search"; 
		} catch (HibernateException ex) {
			return "error";
		} catch (ParseException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/trip_delete", method = RequestMethod.GET)
	public String delTrip(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			tD.delete(tD.getById(id));
			return "redirect:trips";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/routes", method = RequestMethod.GET)
	public String getRoutes(Model model) {
		try {
			List<Route> routes = rD.getAll();
			List<Stop> stops = new ArrayList<Stop>();
			List<String> strings = new ArrayList<String>();
			for (Route r : routes) {
				stops = sD.getByRoute(r);
				strings.add(stops.get(0).getName());
				strings.add(stops.get(stops.size() - 1).getName());
			}
			model.addAttribute("routes", routes);
			model.addAttribute("strings", strings);
			return "routes";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/route", method = RequestMethod.GET)
	public String getRoute(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			Route route = rD.getById(id);
			List<Stop> stops = sD.getByRoute(route);
			model.addAttribute("route", route);
			model.addAttribute("stops", stops);
			return "route";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/route_delete", method = RequestMethod.GET)
	public String delRoute(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			rD.delete(rD.getById(id));
			return "redirect:routes";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/route_edit", method = RequestMethod.GET)
	public String editRoute(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			List<Stop> stops = sD.getAll();
			model.addAttribute("route", rD.getById(id));
			model.addAttribute("rs", sD.getByRoute(rD.getById(id)));
			model.addAttribute("stops", stops);
			return "route_edit";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/route_edit", method = RequestMethod.POST)
	public String editRoutePost(@RequestParam(value="id", required=true) Integer id, 
								@ModelAttribute("route") Route route, 
								@ModelAttribute("nums") ListWrapper<String, String> nums, Model model) {
		try {
			boolean b = true;
			List<String> ints = nums.getList();
			List<Stop> stops = sD.getByRoute(rD.getById(id));
			if (ints.size() == stops.size()) {
				for(int i = 0; i < ints.size(); i++) {
					b = false;
					for(int j = 0; j < stops.size(); j++)
						if (Integer.parseInt(ints.get(i)) == stops.get(j).getId())
							b = true;
					if (!b)
						break;
				}
			}
			else
				b = false;
			if (ints.size() == 0 || b) {
				route.setId(id);
				rD.update(route);
			}
			else {
				rD.create(route);
				for(int i = 0; i < ints.size(); i++) {
					rsD.create(new Route_stops(1, i + 1, rD.getById(route.getId()), sD.getById(Integer.parseInt(ints.get(i)))));
				}
			}
			return "redirect:routes";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/route_add", method = RequestMethod.GET)
	public String addRoute(Model model) {
		try {
			List<Stop> stops = sD.getAll();
			model.addAttribute("stops", stops);
			return "route_add";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/route_add", method = RequestMethod.POST)
	public String addRoutePost(@ModelAttribute("route") Route route, 
							   @ModelAttribute("nums") ListWrapper<String, String> nums, Model model) {
		try {
			rD.create(route);
			List<String> ints = nums.getList();
			for(int i = 0; i < ints.size(); i++) {
				rsD.create(new Route_stops(1, i + 1, rD.getById(route.getId()), sD.getById(Integer.parseInt(ints.get(i)))));
			}
			return "redirect:routes";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/persons", method = RequestMethod.GET)
	public String getPersons(Model model) {
		try {
			List<Person> persons = pD.getAll();
			List<String> strings = new ArrayList<String>();
			SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
			for (Person p : persons) {
				strings.add(sdf.format(p.getBirth()));
			}
			model.addAttribute("persons", persons);
			model.addAttribute("strings", strings);
			return "persons";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person", method = RequestMethod.GET)
	public String getPerson(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			Person person = pD.getById(id);
			String string = null;
			SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
			string = sdf.format(person.getBirth());
			model.addAttribute("person", person);
			model.addAttribute("string", string);
			return "person";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_delete", method = RequestMethod.GET)
	public String delPerson(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			pD.delete(pD.getById(id));
			return "redirect:persons";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_add", method = RequestMethod.GET)
	public String addPerson(Model model) {
		try {
			model.addAttribute("persons", pD.getAll());
			return "person_add";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_add", method = RequestMethod.POST)
	public String addPersonPost(@ModelAttribute("person") Person person, Model model) {
		try {
			pD.create(person);
			return "redirect:persons";
		} catch (HibernateException ex) { 
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_edit", method = RequestMethod.GET)
	public String editPerson(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			model.addAttribute("person", pD.getById(id));
			model.addAttribute("persons", pD.getAll());
			model.addAttribute("birth", new SimpleDateFormat("yyyy-MM-dd").format(pD.getById(id).getBirth()));
			return "person_edit";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_edit", method = RequestMethod.POST)
	public String editPersonpost(@RequestParam(value="id", required=true) Integer id, 
								 @ModelAttribute("person") Person person, Model model) {
		try {
			pD.update(person);
			return "redirect:person?id=" + id.toString();
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_search", method = RequestMethod.GET)
	public String searchPerson(Model model) {
		try {
			model.addAttribute("comps", cD.getAll());
			List<Person> persons = pD.getAll();
			List<String> strings = new ArrayList<String>();
			SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
			for (Person p : persons) {
				strings.add(sdf.format(p.getBirth()));
			}
			model.addAttribute("strings", strings);
			model.addAttribute("persons", persons);
			return "person_search"; 
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_search", method = RequestMethod.POST)
	public String searchPersonPost(@ModelAttribute("person") Person person, 
								   @ModelAttribute("comp") String comp, 
								   @ModelAttribute("min") String min,
								   @ModelAttribute("max") String max,
								   @ModelAttribute("number") String number, Model model) {
		try {
			model.addAttribute("comps", cD.getAll());
			List<String> strings = new ArrayList<String>();
			SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy");
			Company company = null;
			Integer trip = null;
			if (comp != "-1")
				company = cD.getById(Integer.parseInt(comp));
			if (number != null && !number.equalsIgnoreCase(""))
				trip = Integer.parseInt(number);
			List<Person> persons = pD.getByInfo(person.getFirst_name(), 
								   person.getLast_name(), 
								   person.getMiddle_name(),
								   person.getPhone_number(), 
								   person.getEmail(),
								   Integer.parseInt(min),
								   Integer.parseInt(max),
								   company,
								   trip);
			for (Person p : persons) {
				strings.add(sdf.format(p.getBirth()));
			}
			model.addAttribute("strings", strings);
			model.addAttribute("persons", persons);
			return "person_search"; 
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/person_order", method = RequestMethod.GET)
	public String getOrder(@RequestParam(value="id", required=true) Integer id, Model model) {
		try {
			model.addAttribute("pts", ptD.getByPerson(pD.getById(id)));
			model.addAttribute("person", pD.getById(id));
			return "person_order"; 
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/stops", method = RequestMethod.GET)
	public String getStops(Model model) {
		try {
			List<Stop> stops = sD.getAll();
			model.addAttribute("names", stops);
			return "stops";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/stops", method = RequestMethod.POST)
	public String getStopAdd(@ModelAttribute("stop") Stop stop, Model model) {
        try {
			sD.create(stop);
			return "redirect:stops";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/companies", method = RequestMethod.GET)
	public String getCompanies(Model model) {
		try {
			List<Company> comps = cD.getAll();
			model.addAttribute("names", comps);
			return "companies";
		} catch (HibernateException ex) {
			return "error";
		}
	}
	
	@RequestMapping(value = "/companies", method = RequestMethod.POST)
	public String getStopAdd(@ModelAttribute("company") Company company, Model model) {
        try {
			cD.create(company);
			return "redirect:companies";
		} catch (HibernateException ex) {
			return "error";
		}
	}
}